using System;
using UnityEngine;

namespace Tools.UI
{
    [RequireComponent(typeof(OnPointerClickEvent))]
    [AddComponentMenu("Tools/UI/OnPointerDoubleClickEvent")]
    public class OnPointerDoubleClickEvent : MonoBehaviour
    {
        [SerializeField] float doubleClickInterval = 0.3f;
        [SerializeField] UnityEngine.Events.UnityEvent<UnityEngine.EventSystems.PointerEventData> onPointerDoubleClickEvent;
        public UnityEngine.Events.UnityEvent<UnityEngine.EventSystems.PointerEventData> PointerDoubleClickEvent => onPointerDoubleClickEvent;
        private void Awake()
        {
            GetComponent<OnPointerClickEvent>().PointerClickEvent.AddListener(OnPointerClick);
        }

        void Start()
        {
            
        }
        private float _lastClickTime = float.MinValue;
        
        private void OnPointerClick(UnityEngine.EventSystems.PointerEventData eventData)
        {
            if (!enabled) return;
            if (Time.time - _lastClickTime < doubleClickInterval)
            {
                onPointerDoubleClickEvent?.Invoke(eventData);
            }
            _lastClickTime = Time.time;
        }
    }
}

